package view;

import util.dbUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @program: foodApp
 * @description: Admin show all customers' messages
 * @author: PanKer
 * @create: 2021-11-23 19:51
 */

@WebServlet("/A_show_C")
public class A_show_C extends HttpServlet {
    private static final long serialVersionUID = 5057788247394381437L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null || session.getAttribute("admin") == null) {
            resp.sendRedirect(req.getContextPath() + "/html/admin_login.html");
            return;
        }

        resp.setContentType("text/html; charset=utf-8");
        PrintWriter writer = resp.getWriter();
        writer.println(
                " <!DOCTYPE html> " + "\n" +
                        " <html lang='en'> " + "\n" +
                        "  " + "\n" +
                        " <head> " + "\n" +
                        " <meta charset='UTF-8'> " + "\n" +
                        " <meta http-equiv='X-UA-Compatible' content='IE=edge'> " + "\n" +
                        "                      <meta name='viewport' content='width=device-width, initial-scale=1.0'> " + "\n" +
                        "                       <link rel='stylesheet' href='https://cdn.staticfile.org/twitter-bootstrap/4.3.1/css/bootstrap.min.css'> " + "\n" +
                        "                        <script src='https://cdn.staticfile.org/jquery/3.2.1/jquery.min.js'></script>" + "\n" +
                        "                       <script src='https://cdn.staticfile.org/popper.js/1.15.0/umd/popper.min.js'></script> " + "\n" +
                        "                       <script src='https://cdn.staticfile.org/twitter-bootstrap/4.3.1/js/bootstrap.min.js'></script>" + "\n" +
                        "                       <title>adminVerification</title> " + "\n" +
                        "                      <link rel='shortcut icon' href='img/Icon.ico' type='image/x-icon'> " + "\n" +
                        " </head> " + "\n" +
                        " <body>" + "\n" +
                        " <div class='text-center text-dark mt-2'> \n " +
                        " <h2>用户信息</h2> \n " +
                        " </div> \n " +
                        "  \n " +
                        " <div class='container-fluid table-responsive mt-4'> \n " +
                        " <table class='table table-hover table-bordered'> \n " +
                        " <thead> \n " +
                        " <tr> \n " +
                        " <th>用户名</th> \n " +
                        " <th>地址</th> \n " +
                        " <th>邮箱</th> \n " +
                        " <th>手机号</th> \n " +
                        " <th>生日</th> \n " +
                        " <th>UID</th> \n " +
                        " </tr> \n " +
                        " </thead> \n " +
                        " <tbody> \n "
        );

        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;

        try {
            conn = dbUtil.getConn();
            String sql = "SELECT * FROM customer";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();


            while (rs.next()) {
                String username = rs.getString("UserName");
                String address = rs.getString("Address");
                String email = rs.getString("Email");
                String tel = rs.getString("Telephone");
                String date = rs.getString("Date");
                int id = rs.getInt("Id");

                writer.println(
                        " <tr> \n " +
                                " <td>" + username + "</td> \n " +
                                " <td>" + address + "</td> \n " +
                                " <td>" + email + "</td> \n " +
                                " <td>" + tel + "</td> \n " +
                                " <td>" + date + "</td> \n " +
                                " <td>" + id + "</td> \n " +
                                " </tr> \n "
                );
            }

        } catch (ClassNotFoundException e) {
            System.out.println("ClassNotFoundException错误");
            e.printStackTrace();
        } catch (SQLException e) {
            System.out.println("SQLException错误");
            e.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            try {
                dbUtil.closeConn(conn);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        writer.println(
                " </tbody> \n " +
                        " </table> \n " +
                        " </div> \n " +
                        "  \n " +
                        " </body> \n " +
                        " </html> \n "
        );
    }
}
